<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" 
			cancelButton="none" :value="kw?kw:''"></uni-search-bar>
		</view>
		
		<view class="sugg-list" v-if="searchResults.length!==0">
			<view class="sugg-item" v-for="(item,i) in searchResults" :key="i">
				<view class="message-list">{{item}}</view>
<!-- 				<uni-icons type="arrowright" size="16"></uni-icons> -->
			</view>
			
		</view>
		
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size='17' @click="clear"></uni-icons>
			</view>
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,i) in historys" :key="i" @click="fill(item)"></uni-tag>
			</view>
		</view>
	</view>
	
	
</template>

<script>
import { computed } from "vue";
	export default {
		data() {
			return {
				searchResults:[],
				timer:null,
				kw:'',
				historyList:[],
				// count:0
			};
		},
		onLoad() {
			this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
		},
		onPullDownRefresh(){
			this.searchResults=[]
			this.kw=''
			// this.count=0
			setTimeout(function () {
						uni.stopPullDownRefresh();
					}, 500);
		},
		onReachBottom(){
			this.getSearchList()
		},
		methods:{
			// 历史记录自动填充
			fill(item){
				this.kw=item
			},
			// 刚刚输入时触发
			input(e){
				clearTimeout(this.timer)
				// console.log(typeof(this.searchResults))
				this.timer=setTimeout(()=>{
					this.kw=e
					this.getSearchList()
					this.saveSearchHistory()
				},500)
			},
			//列出推荐的项目
			getSearchList(){
				if(this.kw===''){
					this.searchResults=[]
					return
				}
				var _this = this
				// var arr = []
				uni.request({
					url:'http://47.97.5.12:5000//api//data',
					method:"POST",
					header:{
					"Content-Type": "application/x-www-form-urlencoded"	
					},
					data:{
						input:JSON.stringify(this.kw)
					},
					success:function(res){
						if(res.statusCode===200){
							_this.searchResults=res.data.output.split("\n")
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
				console.log(this.searchResults)
			},
			// 当点击项目时，跳转到详情页
			gotoDetail(item){
				uni.navigateTo({
					url:'/pages/detail/detail?message='+item
				})
			},
			//搜索之后保存历史记录到本地
			saveSearchHistory(){
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList=Array.from(set)
				// 存储数据，保存到本地
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			//点击清空按钮之后，清空聊天记录
			clear(){
				this.historyList=[]
				uni.setStorageSync('kw','[]')
			},
		},
		
		computed:{
			historys(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
.search-box{
	position: sticky;
	top:0;
	z-index: 999;
}
.sugg-list{
	padding: 0 5px;
	.sugg-item{
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-size: 18px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		.message-list{
			// white-space: nowrap;
			// overflow: hidden;
			// text-overflow: ellipsis;
		}
	}
}
.history-box{
	padding: 0 5px;
	.history-title{
		display: flex;
		justify-content: space-between;
		height:40px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #efefef;
	}
	.history-list{
		display: flex;
		flex-wrap: wrap;
		
		.uni-tag{
			margin-top: 5px;
			margin-right: 5px;
		}
	}
}
</style>
